package com.xiaoma.nio;

import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * MappedByteBuffer 可以让文件直接在内存修改，操作系统不需要拷贝一次
 */
public class MappedByteBufferDemo {
    public static void main(String[] args) throws Exception {
        RandomAccessFile file = new RandomAccessFile("xiaoma.txt", "rw");
        FileChannel channel = file.getChannel();
        //FileChannel.MapMode.READ_WRITE 读写模式，0 可以直接修改的起始位置，5 映射到内存的大小
        //从0开始 修改5个字节 索引值0~4
        MappedByteBuffer mappedByteBuffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, 5);
        mappedByteBuffer.put(0,(byte) 'b');
        mappedByteBuffer.put(3,(byte) 'b');
        file.close();
        System.out.println("修改成功");
    }
}
